<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增数据源')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-source-add">
            <div class="col-sm-6">
               <div class="form-group">
                   <label class="col-sm-4 control-label is-required">数据源编码：</label>
                   <div class="col-sm-8">
                       <input name="code" class="form-control" type="text" placeholder="请输入数据源编码" maxlength="200"  required>
                   </div>
               </div>
           </div>
           <div class="col-sm-6">
               <div class="form-group">
                   <label class="col-sm-4 control-label is-required">数据源名称：</label>
                   <div class="col-sm-8">
                       <input name="name" class="form-control" type="text" placeholder="请输入数据源名称" maxlength="500"  required>
                   </div>
               </div>
           </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">驱动：</label>
                    <div class="col-sm-10">
                        <input name="dbDriver" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">地址：</label>
                    <div class="col-sm-10">
                        <textarea name="dbUrl" class="form-control"></textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">数据库名称：</label>
                    <div class="col-sm-10">
                        <input name="dbName" class="form-control" type="text">
                    </div>
                </div>
            </div>
            
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">用户名：</label>
                    <div class="col-sm-8">
                        <input name="dbUsername" class="form-control" type="text" placeholder="请输入用户名" maxlength="200" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">密码：</label>
                    <div class="col-sm-8">
                        <input name="dbPassword" class="form-control" type="text" placeholder="请输入密码" maxlength="500"  required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">描述：</label>
                    <div class="col-sm-10">
                        <textarea name="remark" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
            <h4 class="form-header h4">属性配置</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
					    <table id="bootstrap-table"></table>
					</div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "sys/dataSource"
        
        $(function() {
	    	// 初始化数据, 可以由后台传过来
	    	var data = [
	    	{
	    		id: "100",
	    		name: "名称1",
            	weight: "100",
            	type: "1",
	    	},
	    	{
	    		id: "101",
	    		name: "商品名称2",
            	weight: "50",
            	type: "0",
	    	}];
		    var options = {
		    	data: data,
                pagination: false,
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
		        columns: [{
		            checkbox: true
		        },
		        {
		        	field: 'index',
		        	align: 'center',
		        	title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	var columnId = $.common.sprintf("<input type='hidden' name='goods[%s].id' value='%s'>", index, row.id);
                    	return columnIndex + $.table.serialNumber(index) + columnId;
                    }
                },
		        {
		            field: 'name',
		            align: 'center',
		            title: '属性名称',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='goods[%s].name' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'weight',
		            align: 'center',
		            title: '属性值',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control goodsWeight' type='text' name='goods[%s].weight' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'type',
		            align: 'center',
		            title: '商品种类',
		            formatter: function(value, row, index) {
		        		var data = [{ index: index, type: value }];
		                return $("#goodsTypeTpl").tmpl(data).html();
                    }
		        },
		        {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                    	var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
		    };
		    $.table.init(options);
		});
        $("#form-source-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-source-add').serialize());
            }
        }
        
        function addRow() {
        	var count = $("#" + table.options.id).bootstrapTable('getData').length;
        	var row = {
                index: $.table.serialNumber(count),
            	name: "",
            	weight: "",
            	type: "",
            }
        	sub.addRow(row);
        }
    </script>
</body>
</html>
<!-- 商品类型 -->
<script id="goodsTypeTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='goods[${index}].type'>
    <option value="">所有</option>
    <option value="0" {{if type==="0"}}selected{{/if}}>寒性</option>
    <option value="1" {{if type==="1"}}selected{{/if}}>热性</option>
</select>
</div>
</script>